 
    /** 
     * 文件名：Test.java 
     * 
     * 版本信息： 
     * 日期：2015年1月16日 
     * Copyright 足下 Corporation 2015 
     * 版权所有 
     * 
     */ 
    
package com.qyer.util.log.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.qyer.util.log.common.Constants;
import com.qyer.util.log.entity.AppTrackLog;
import com.qyer.util.log.entity.City;
import com.qyer.util.log.entity.Country;
import com.qyer.util.log.entity.Guide;
import com.qyer.util.log.service.LogService;
import com.qyer.util.log.service.impl.LogService4NginxImpl;
import com.qyer.util.log.util.AppUgcUtil;
import com.qyer.util.log.util.HiveFormatUtil;
import com.qyer.util.log.util.LogFatory;



public class CopyOfCopyOfTestAppTrackNginx  {
	
	static HashMap<String, City> cityMaps = new HashMap<String, City>();
	static HashMap<String, Country> countryMaps = new HashMap<String, Country>();
	//    
	private static HashMap <String, Guide> guideMap = new HashMap<String, Guide>();
    private static HashMap<String,String> mapApp = new HashMap<String,String>();// 存储app规则

		public static void main(String[] args) {
			
			getMapApp();
			getGuideMap();
			getCityMaps();
			
			String fileName="/Users/sunhongxi/Documents/sshfileupdown/115.182.69.217_log.1462896607316";
			LogService lli = (LogService)LogFatory.getInstance(LogService4NginxImpl.class.getName());
			
			File file = new File(fileName);
	        BufferedReader reader = null;
	        try {
	        	
	        	Pattern pattern1 = Pattern.compile("^[a-zA-Z0-9-]+$");
	    		Pattern pattern2 = Pattern.compile("^[a-z_]+$");
	    		Pattern p_ra_n_referer=Pattern.compile("\"ra_n_referer\";s:[0-9]+:\"(.*)\";");
	    		Pattern p_ra_n_model=Pattern.compile("\"ra_n_model\";s:[0-9]+:\"(.*)\";");
	    		Pattern p_ra_n_page=Pattern.compile("\"ra_n_page\";s:[0-9]+:\"(.*)\";");
	    		Pattern p_params=Pattern.compile("\"params\";s:[0-9]+:\"(.*)\";");
	        	
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            int i=0 , a =0 ; 
	            
	            long s1= System.currentTimeMillis(); 
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	            	
	            	if(!tempString.contains("country_detail")){
	            		continue; 
	            	}
	            	
//	            	 System.out.println(tempString);
	                 String [] ls = StringUtils.split(tempString , Constants.FS);
	                 
	                 AppTrackLog ll =lli.getAppTrackFormat(tempString);
	            	 if(ll!=null&&"/open".equals(ll.getType())){
	                	 if (ll.getDeviceid()!=null && ll.getDeviceid().length()>0 && !"".equals(ll.getDeviceid().trim())&&ll.getTrackTime()!=null&&!"".equals(ll.getTrackTime())){
	                		 //deviceid clientid 格式校验
	                         Matcher m1 = pattern1.matcher(ll.getDeviceid());
	                         Matcher m2 = pattern2.matcher(ll.getClientId());
	                		 if(m1.matches()&&m2.matches()){
	                			 
		                		 //设置UGC 相关属性 
		                		AppUgcUtil acu =new AppUgcUtil(); 
		              			java.util.Map<String,String> ugctypeMap=acu.apireplace(ll.getApiName(), ll.getInfo(), mapApp, cityMaps, countryMaps, guideMap);
		                		
		              			Long ts =Long.parseLong(ll.getTimeStamp()); 
		                		ts=ts/1000;
		                		
		                		
		                		StringBuilder  outStr = new StringBuilder ("");
		             			
		             			outStr.append(HiveFormatUtil.getFeild(ll.getUserId())).append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getDeviceid())).append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ts.toString())).append(Constants.FS);
		             			outStr.append("-").append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ts.toString())).append(Constants.FS);
		             			outStr.append("+08:00").append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getUserIp())).append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getLon())).append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getLat())).append(Constants.FS);
		             			
		             			if(ugctypeMap==null||ugctypeMap.get("api_path")==null){
		             				outStr.append("-").append(Constants.FS);
		             			}else{
		             				outStr.append(HiveFormatUtil.getFeild(ugctypeMap.get("api_path"))).append(Constants.FS);
		             			}
		             			
		             			outStr.append("-").append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getType())).append(Constants.FS);
		             			outStr.append("-").append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getParamsMap().get("ra_arg"))).append(Constants.FS);
//		             			outStr.append(HiveFormatUtil.getFeild(ll.getInfo())).append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getUserAgent())).append(Constants.FS);
		             			//outStr.append("-").append(Constants.FS);  //之前channel为-，现改为获取channle字段  王振 改
		             			
		             			/*****************王振改 start**********************/
		             			if(ugctypeMap==null||ugctypeMap.get("channel")==null){
		             				outStr.append("-").append(Constants.FS);
		             			}else{
		             				outStr.append(HiveFormatUtil.getFeild(ugctypeMap.get("channel"))).append(Constants.FS);
		             			}
		             			/*****************王振改 stop**********************/
		             			
		             			if(ugctypeMap==null||ugctypeMap.get("ugc_type")==null){
		             				outStr.append("-").append(Constants.FS);
		             			}else{
		             				outStr.append(HiveFormatUtil.getFeild(ugctypeMap.get("ugc_type"))).append(Constants.FS);
		             			}
		             			
		             			outStr.append(HiveFormatUtil.getFeild(ll.getPlatform())).append(Constants.FS);
		             			
		             			
		             			
		             			if(ugctypeMap==null||ugctypeMap.get("ugc_content")==null){
		             				outStr.append("-").append(Constants.FS);
		             			}else{
		             				outStr.append(HiveFormatUtil.getFeild(ugctypeMap.get("ugc_content"))).append(Constants.FS);
		             			}
		             			
		             			
		             			
		             			outStr.append(HiveFormatUtil.getMapFeild(ll.getExtParamMap())).append(Constants.FS);
		             			outStr.append("-").append(Constants.FS);
		             			outStr.append("-").append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getTrackTime())).append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getHour())).append(Constants.FS);
		             			outStr.append("-").append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getClientId())).append(Constants.FS);
		             			outStr.append(HiveFormatUtil.getFeild(ll.getInfo())).append(Constants.FS);
		             			outStr.append("-");
		             			
		             			System.out.println(ugctypeMap.get("ugc_type")+"\t"+ugctypeMap.get("api_path")+"\t"+ll.getTrackTime());
		                	 }else{
		                		 System.out.println(line);
		                	 }
	                	 }
	            	 }
	            }
	            long s2= System.currentTimeMillis(); 
	            System.out.println("a=="+a);
	            System.out.println("i=="+i);
	            System.out.println("time ===="+ (s2-s1)/1000);
	            reader.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
			
			
		}
		
		public static Map<String , String > getMapApp(){
			Map<String , String > ugc_typeMaps = new HashMap<String,String>();
			
			String fileName="/Users/sunhongxi/Documents/sshfileupdown/clean_track/app_trans.xml";

			File file = new File(fileName);
	        BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            int i=0 , a =0 ; 
	            String platform ="";
	            long s1= System.currentTimeMillis(); 
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	            	if(tempString.startsWith("//")){
						continue;
					}else{
						String[] array = tempString.split("@@");
						if (array.length > 0) {
							String api_path=array[0].split("##")[0];
							mapApp.put(api_path, array[0]);
						}
					}
	            }
	            reader.close();
	            return ugc_typeMaps;
	        } catch (IOException e) {
	            e.printStackTrace();
	            return null; 
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
		}
		
		public static Map<String , String > getGuideMap(){
			Map<String , String > ugc_typeMaps = new HashMap<String,String>();
			
			String fileName="/Users/sunhongxi/Documents/sshfileupdown/clean_track/guide_list.csv";

			File file = new File(fileName);
	        BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            int i=0 , a =0 ; 
	            String platform ="";
	            long s1= System.currentTimeMillis(); 
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	                line++;
	                
	                String[] array = tempString.split("#\\|~");
	                if (array.length != 2) {
	                    continue;
	                }
	                Guide guide = new Guide();
	                guide.setId(array[0]);
	                guide.setName( array[1].replace(" ", "_"));
	                guideMap.put(guide.getId(), guide);
	            }
	            reader.close();
	            return ugc_typeMaps;
	        } catch (IOException e) {
	            e.printStackTrace();
	            return null; 
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
		}
		
		public static Map<String , String > getCityMaps(){
			Map<String , String > cityMap = new HashMap<String,String>();
			
			String fileName="/Users/sunhongxi/Documents/sshfileupdown/clean_track/city_country_list.csv";

			File file = new File(fileName);
	        BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            int i=0 , a =0 ; 
	            
	            long s1= System.currentTimeMillis(); 
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	                line++;
	                String[] array = tempString.split("#\\|~");
	                if (array.length < 6) {
	                    System.out.println(line + array.length);
	                    continue;
	                }
					String cityName;
					if (array[2].length() > 0 && !array[2].equals("None")) {
						cityName = array[2];
					} else {
						cityName = array[1];
					}
					String cityID = array[0];
					String countryName = array[4];
					String countryID = array[3];

					//cityNameMaps.put(cityName, cityID);
					//countryNameMaps.put(countryName, countryID);
					
	                City city = new City();
	                city.setCityid(array[0]);
	                if (array[2].length() > 0 && !array[2].equals("None")) {
	                    city.setCityname(array[2].replace(" ", "_"));
	                } else {
	                    city.setCityname( array[1].replace(" ", "_"));
	                }


	                Country country = countryMaps.get(array[3]);
	                if (country == null) {
	                    country = new Country();
	                    country.setCountryid(array[3]);
	                    country.setCountryname(array[4].replace(" ", "_")); 
	                    countryMaps.put(country.getCountryid(), country);
	                }
	                city.setCountry(country);
	                cityMaps.put(city.getCityid(), city);
	            }
	            reader.close();
	            return cityMap;
	        } catch (IOException e) {
	            e.printStackTrace();
	            return null; 
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
		}
		
		
}
